

package tractorlk.drops;

import java.util.List;
import java.awt.Polygon;
import java.awt.geom.Point2D;
import tractorlk.Drawable;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Collections;
import java.awt.Dimension;

public class Hand
{

    List<Card> fullHand;// = new ArrayList<Card>();
    List<Card> currentHand;// = new ArrayList<Card>();
    CardComparator compare;
    Card justDrew;
    

    public Hand()
    {
        fullHand = new ArrayList<Card>();
        currentHand = new ArrayList<Card>();
    }

    public Hand(List<Card> dealt)
    {
        fullHand = dealt;
        currentHand = dealt;
    }

    public Hand(Deck theDeck, int handnum)
    {
        if(handnum == 0)
            currentHand = theDeck.seperateKitty();
        else
        {
            fullHand = theDeck.deal(handnum);
            currentHand = fullHand;
        }
    }

    public void updateHand(Dimension size, Graphics g)
    {
            int dx = 0;
            if (!currentHand.isEmpty())
                dx = size.width/2/currentHand.size();
            int x = size.width/2-dx/2*(currentHand.size()-1)-50;
            for(Card s : currentHand)
            {
                s.setPosition(x, size.height*4/5);
                x = x+ dx;
            }
    }


    public void showHand(Dimension size, int position, Graphics g)
    {
        if(position ==1)    //so far 1 = south
        {
            int dx = 0;
            if (!currentHand.isEmpty())
                dx = size.width/2/currentHand.size();
            int x = size.width/2-dx/2*(currentHand.size()-1)-50;
            for(Card s : currentHand)
            {
                s.setPosition(x, size.height*4/5);
                x = x+ dx;
                s.draw(g);

            }
        }
        if(position ==5)    //so far 5 = southHand
        {
            int dx = 0;
            if (!currentHand.isEmpty())
                dx = size.width/5/currentHand.size();
            int x = size.width/2-dx/2*(currentHand.size()-1);
            for(Card s : currentHand)
            {
                s.setPosition(x, size.height/2);
                x = x+ dx;
                s.draw(g);

            }
        }
        if(position ==6)    //so far 6 = eastHand
        {
            int dx = 0;
            if (!currentHand.isEmpty())
                dx = size.width/5/currentHand.size();
            int x = size.width*3/5-dx/2*(currentHand.size()-1);
            for(Card s : currentHand)
            {
                s.setPosition(x, size.height*3/10);
                x = x+ dx;
                s.draw(g);

            }
        }
        if(position ==7)    //so far 7 = northHand
        {
            int dx = 0;
            if (!currentHand.isEmpty())
                dx = size.width/5/currentHand.size();
            int x = size.width/2-dx/2*(currentHand.size()-1);
            for(Card s : currentHand)
            {
                s.setPosition(x, size.height/6);
                x = x+ dx;
                s.draw(g);

            }
        }
        if(position ==8)    //so far 8 = westHand
        {
            int dx = 0;
            if (!currentHand.isEmpty())
                dx = size.width/5/currentHand.size();
            int x = size.width/5-dx/2*(currentHand.size()-1);
            for(Card s : currentHand)
            {
                s.setPosition(x, size.height/3);
                x = x+ dx;
                s.draw(g);

            }
        }
        if(position ==9)    //so far 9 = debug
        {
            int dx = 0;
            if (!currentHand.isEmpty())
                dx = size.width/currentHand.size()-1;
            int x = size.width/2-dx/2*(currentHand.size()-1)-50;
            for(Card s : currentHand)
            {
                s.setPosition(x, size.height*1/20);
                x = x+ dx;
                s.draw(g);

            }
        }
    }

    public void sortHand(int rank, int suit)
    {
        compare = new CardComparator(rank, suit);
        Collections.sort(currentHand, compare);
        
    }

    public void unsortHand()
    {
        for (Card c: currentHand)
            c.resetGameID();
    }

    public List<Card> getHand()
    {
        return currentHand;
    }

    public int getSize()
    {
        return currentHand.size();
    }

    public void play(Card c, Hand dest)
    {
        currentHand.remove(c);
        dest.add(new Card(c));
    }

    public void remove(Card c)
    {
        currentHand.remove(c);
    }
    public void remove(int c)
    {
        int index = 0;
        int max = currentHand.size();
        while(index < max)
        {
            if(currentHand.get(index).getID()%54 == c)
            {
                currentHand.remove(index);
                return;
            }
            index++;
        }
    }

    public void add(Card c)
    {
        currentHand.add(new Card(c));
    }

    public void add2(Card c)    //for toggling cards in the selected hand, we dont want to deep copy
    {
        currentHand.add(c);
    }


    public void unset()
    {
        for(Card c  :   currentHand)
            c.unset();
    }

    public int points()
    {
        int total = 0;
        for(Card c  : currentHand)
        {
            if(c.getRank() == 5)
                total = total + 5;
            else if (c.getRank() == 10)
                total = total + 10;
            else if (c.getRank() == 13)
                total = total + 10;
        }
        return total;
    }

    public int tractorSize(int suit, int rank)
    {
        int size = currentHand.size();
        int returnSize = size;
        int currentID;
        if(size < 4 || size%2 == 1)
            return -1;
        sortHand(suit, rank);
        currentID = currentHand.get(size-1).getGameID() + 1;
        int tempID;
        while(size > 0)
        {
            tempID = currentHand.get(size-1).getGameID();
            if(tempID != currentHand.get(size-2).getGameID())
                return -1;
            if(tempID != currentID - 1)
                return -1;
            currentID = tempID;
            size = size -2;
        }
        System.out.println("Tractor size: " + returnSize);
        return returnSize;

    }

    public boolean isSingle()
    {
        if (currentHand.size() == 1)
            return true;
        else
            return false;
    }

    public boolean isDouble()
    {
        if(currentHand.size() != 2)
            return false;
        if( currentHand.get(0).getID()%54 != currentHand.get(1).getID()%54)
            return false;
        return true;
    }

    public boolean hasSuit(int suit, int rank, int trump)
    {
        for(Card c : currentHand)
            if((c.getSuit() == suit && c.getRank() != rank)
                || ((suit==trump) && ((c.getRank() == rank) || c.getRank() > 14) ))
                return true;
        return false;
    }

    public int hasSuit(int suit, int rank, int trump, int dummy)
    {
        int count = 0;
        for(Card c : currentHand)
        {
            if((c.getSuit() == suit && ((suit == trump) || c.getRank() != rank))
                || ((suit==trump) && ((c.getRank() == rank) || c.getRank() > 14) ))
                count++;
        }
        return count;
    }

    public void clear()
    {
        currentHand.clear();
    }

    public boolean isEmpty()
    {
        return currentHand.isEmpty();
    }

    public boolean hasHand(int type, int suit, int trump, int rank)
    {
        int prevID = -1;
        int prevDouble = -2;
        for(Card c : currentHand)
        {
            if(((c.getSuit() != suit) && !(suit == trump && c.getSuit() == 5) ) || ((suit !=trump) && c.getRank() == rank )) //if doesnt have suit, its done
                ;
            else
            {
                if(type == 1)       //if has suit, then has a single card
                    return true;
                if(c.getID()%54 == prevID)  //if same ID as prev
                {
                    if(type == 2)           //if double its true
                    {
                        System.out.println("Has double of: " + c );
                        return true;
                    }
                    else
                    {
                        if(prevID == prevDouble+1 && type == 4) //has tractor
                        {
                            System.out.println("Has tractor of: " + c );
                            return true;
                        }
                        prevDouble = prevID;
                    }
                }
                prevID = c.getID()%54;
            }
        }

        return false;
    }

    //assumes sorted hand
    public Card getMaxCard()
    {
        int size = currentHand.size();
        System.out.println("Max Card: " + currentHand.get(size-1));
        return currentHand.get(size-1);
    }

    public int hasDoubles(int suit, int trumpSuit, int rank)
    {
        int prevID = -1;
        int doubleCount = 0;
        for(Card c : currentHand)
        {
            if(((c.getSuit() != suit) && !(suit == trumpSuit && c.getSuit() == 5)&& !(suit == trumpSuit && c.getRank() == rank) ) || ((suit !=trumpSuit && suit != 5) && c.getRank() == rank )) //if doesnt have suit, its done
                ;
            else
            {
                if(c.getID()%54 == prevID)  //if same ID as prev
                {
                    doubleCount++;
                }
                prevID = c.getID()%54;
            }
        }

        return doubleCount;
    }

    public void populateHighCards()
    {
        for(int i=0;i<108;i++)
        {
            currentHand.add(new Card(i));
        }
    }

    public int highCardCount(Hand highCards, int trumpSuit, int trumpRank)
    {
        sortHand(trumpRank, trumpSuit);

        int prevID = -1;
        int count = currentHand.size();
        if(count == 1)
            return 0;

        int lowest = currentHand.get(0).getGameID();
        if(lowest == currentHand.get(1).getGameID())
            count--;
        int suit = lowest/13+1;
        if(lowest > 52) //if a trump , there is no card of next suit to return from
            suit = 10;  //13*10 is alot bigger than max GameID of 65
        for(Card c : highCards.getHand())
        {
            if(c.getGameID() >= suit*13)
                break;
            else if (c.getGameID() > lowest)    //if not a high card series
            {
                count--;
                if(count == 0)
                {
                    System.out.println("not a high card series cuz : " + currentHand.get(0) + " < " + c);
                    return 0;
                }
            }
        }

        int highCount = 0;
        for(Card c : currentHand)
        {
            if(c.getID()%54 == prevID)  //if same ID as prev
            {
                highCount = highCount +100;
            }
            prevID = c.getID()%54;
            highCount++;
        }
        return -highCount;   //gona be a pain!!
    }

}
